package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ConnectException;

public class ClientApp {

    private InputConsole ic;
    private BufferedReader br;
    private ClientProxy cp;
    private CommandInterpreter ci;

    public ClientApp(String... args) throws ConnectException {

        String serverAddress = System.getProperty("server", "192.168.1.108");

        int serverPort = 7777;
        try {
            serverPort = Integer.parseInt(System.getProperty("server_port", "7777"));
        } catch (NumberFormatException e) {
            System.out.println("Warning: using default port 7777");
        }

        ic = new InputConsole();
        ic.getArguments(args);
        ic.connectWithOutputConsole();

        br = new BufferedReader(new InputStreamReader(System.in));
        cp = new ClientProxy(new ClientTransport(serverAddress, serverPort));
        ci = new ClientCommandInterpreter(br, cp);
        new Thread(new ClientResponseThread(this, cp.getTransport())).start();
    }

    public void start() {
        ci.start();
    }

    public static void main(String... args) throws ConnectException {
        ClientApp app = new ClientApp(args);
        app.start();
    }

    public void print(String substring) {
        ic.sendOutput(substring);
    }
}
